package com.server.model;

import com.common.Message;
import com.common.MessageType;
import com.server.controller.AcceptedSocketThreadManager;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;

/**
 *
 */
/*
功能:服务器和某个客户端的通信线程
 */
public class AcceptedSocketThread extends Thread{
    Socket s;


    public AcceptedSocketThread(Socket s){
        //把服务器与该客户端的连接,赋值给s
        this.s = s;
    }
    public  void  notifyOther(Message message){
        //得到所有在线的人的线程
        HashMap map = AcceptedSocketThreadManager.map;
        Iterator it = map.keySet().iterator();

        while (it.hasNext()){
            try {
                //取出在线人的id号
                String onlineUserId = it.next().toString();
                //如果没有发送者说明是系统发的,那么发送者会为null,如果是系统发的那不能跳过
                //发送者等于接受者,就跳过
                if (message.getSender()!=null&&message.getSender().equals(onlineUserId)){
                    System.out.println(message.getMesType());
                    continue;
                }

                //取出在线人的id号取出socket
                ObjectOutputStream oos = new ObjectOutputStream(AcceptedSocketThreadManager.getClientThread(onlineUserId).s.getOutputStream());
                //getter有两种情况,一种是没有,一种是all
                //如果不是all,代表这是一个要通知在线或者不在线的,不会打开窗口,只需要设置接收方,让接收方知道最新的情况
                //如果是all,代表以后要打开那个all的窗口,所以发送者要是all,这样被接受的时候接收方会打开接收方AndAll窗口
                /*if(message.getMesType().endsWith("All")){
                    message.setSender(message.getGetter());
                }*/
                //上面的六行注释没用,无论是不是发给all的,都设置接受者,为当前遍历到的用户
                message.setGetter(onlineUserId);
                oos.writeObject(message);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
    @Override
    public void run() {
        while(true){
            //该线程接受客户端的信息
            try{
                ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
                Message m = (Message)ois.readObject();
                //对取得的消息进行类型判断,然后做相应处理
                System.out.println(m);
                if (m.getMesType().equals(MessageType.message_common)||m.getMesType().equals(MessageType.message_file)){
                    //不是文本就是文件
                    System.out.println(m.getSender()+" 向 "+m.getGetter()+" 发送 "+m.getText());
                    //转发任务
                    //取得接收人的通讯线程
                    AcceptedSocketThread scct = AcceptedSocketThreadManager.getClientThread(m.getGetter());
                    ObjectOutputStream oos = new ObjectOutputStream(scct.s.getOutputStream());
                    oos.writeObject(m);
                }else if(m.getMesType().equals(MessageType.message_get_onLineFriend)){
                    System.out.println(m.getSender()+"要他的好友");
                    Message m2 = new Message();
                    m2.setMesType(MessageType.message_ret_onLineFriend);
                    String res = AcceptedSocketThreadManager.getAllOnlineUserId();
                    Message message = new Message();
                    message.setText(res);
                    message.setMesType(MessageType.message_ret_onLineFriend);
                    notifyOther(message);
                    m2.setText(res);
                    m2.setGetter(m.getSender());
                    ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
                    oos.writeObject(m2);
                }else if(m.getMesType().equals(MessageType.message_close)){
                    System.out.println("用户 "+m.getSender()+" 下线了");
                    //下线更新服务器在线用户情况(改IsLogin为0)
                    new UserService().loadExit(m.getSender());

                    AcceptedSocketThreadManager.remove(m.getSender());
                    String res = AcceptedSocketThreadManager.getAllOnlineUserId();
                    Message message = new Message();
                    message.setText(res);
                    message.setMesType(MessageType.message_ret_onLineFriend);
                    notifyOther(message);
                    s.close();
                    break;
                }else if (m.getMesType().equals(MessageType.message_commonToAll)||m.getMesType().equals(MessageType.message_fileToAll)){
                    System.out.println(m.getSender()+" 向 "+m.getGetter()+" 发送 "+m.getText());
                    notifyOther(m);
                }else if (m.getMesType().equals(MessageType.message_shakeWindow)) {
                    System.out.println(m.getSender() + "发送了窗口抖动.");
                    AcceptedSocketThread scct = AcceptedSocketThreadManager.getClientThread(m.getGetter());
                    ObjectOutputStream oos = new ObjectOutputStream(scct.s.getOutputStream());
                    oos.writeObject(m);
                }else if (m.getMesType().equals(MessageType.message_shakeWindowToALL)) {
                    System.out.println(m.getSender() + "发送了窗口抖动.");
                    notifyOther(m);
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
}
